<?php

class UsuarioDAO{

	private $tableName = 'tb_usuario';
	private $conn;
	private $sqlGenerator;
	private $prefixo;

	public function __construct(){

		$this->conn  = new CONEXAO();

	}

	public function getTableName(){
		return $this->tableName;
	}

	public function setTableName($tableName){
		$this->tableName=$tableName;
	}

	public function getConn(){
		return $this->conn;
	}

	public function setConn($conn){
		$this->conn=$conn;
	}

	public function getSqlGenerator(){
		return $this->sqlGenerator;
	}

	public function setSqlGenerator($sqlGenerator){
		$this->sqlGenerator=$sqlGenerator;
	}

	public function getPrefixo(){
		return $this->prefixo;
	}

	public function setPrefixo($prefixo){
		$this->prefixo=$prefixo;
	}

	public function inserir($obj){
		$this->conn->Conecta();
		$id = $this->conn->maxId($this->tableName) + 1;
		$this->conn->Desconecta();
		
		$obj->setId($id);
		$login = $obj->getLogin();
		$senha = $obj->getSenha();
		$siape = $obj->getSiape();
		$colegiado = $obj->getColegiado();
		
		$consulta = "INSERT INTO ".$this->tableName." (id, login, senha, siape, colegiado) VALUES ('$id', '$login', '$senha', '$siape', '$colegiado')";
		//echo $consulta;
		
		$this->conn->Conecta();
		$this->conn->Executa($consulta);
		$this->conn->Desconecta();
	}

	public function atualizar($obj){
		$id = $obj->getId();
		$login = $obj->getLogin();
		$senha = $obj->getSenha();
		$siape = $obj->getSiape();
		$colegiado = $obj->getColegiado();
		
		$consulta = "UPDATE ".$this->tableName." SET login='$login', senha='$senha', siape='$siape', colegiado='$colegiado' WHERE id='$id'";
		//echo $consulta;
		
		$this->conn->Conecta();
		$this->conn->Executa($consulta);
		$this->conn->Desconecta();
	}

	public function apagar($id){
		$consulta = "DELETE FROM ".$this->tableName." WHERE id=".$id;
		//echo $consulta;
		
		$this->conn->Conecta();
		$this->conn->Executa($consulta);
		$this->conn->Desconecta();
	}

	public function geraWherePesquisa(){

		//TODO

	}

	public function contem($obj){
		$login = $obj->getLogin();
		//echo $login;
		
		$consulta = "SELECT * FROM ".$this->tableName." WHERE login='$login'";
		//echo $consulta;

		$this->conn->Conecta();
		$res = $this->conn->getResultAsVector($consulta);
		$this->conn->Desconecta();
		
		if (count($res) > 0)
			return 1;
		else
			return 0;
	}

	public function buscar($obj){
		if ($obj->getId() != 0) {
			$id = $obj->getId();
			$consulta = "SELECT * FROM ".$this->tableName." WHERE id='$id'";
		}
		elseif ($obj->getLogin() != "") {
			$login = $obj->getLogin();
			$consulta = "SELECT * FROM ".$this->tableName." WHERE login='$login'";
		}
		//echo $consulta;
		
		$this->conn->Conecta();
		$res = $this->conn->getResultAsVector($consulta);
		$this->conn->Desconecta();
		
		if (count($res) > 0) {
			$obj = Usuario::construct($res[0]);
			$obj->setId($res[0]['id']);
			return $obj;
		}
		else
			return null;
	}

	public function buscarTodos(){
		$consulta = "SELECT * FROM ".$this->tableName;
		
		$this->conn->Conecta();
		$res = $this->conn->getResultAsVector($consulta);
		$this->conn->Desconecta();
		
		return $res;
	}

	public function buscarTodosSerach($colegiado){
		$consulta = "SELECT * FROM ".$this->tableName. " WHERE colegiado='$colegiado'";
		
		$this->conn->Conecta();
		$res = $this->conn->getResultAsVector($consulta);
		$this->conn->Desconecta();
		
		return $res;
	}
}

?>